package com.wash.shoes.controller.pc;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.domain.dto.RegionDistributorDto;
import com.wash.shoes.domain.vo.RegionDistributorVo;
import com.wash.shoes.entity.RegionDistributorPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.RegionDistributorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ljg
 * @since 2024-10-11
 */
@Api(tags = {"PC-分销-分销用户配置"})
@RestController
@RequestMapping("/regionDistributor")
public class RegionDistributorController {

    @Resource
    RegionDistributorService regionDistributorService;

    @PostMapping("/list")
    @ApiOperation("分销用户列表和客户表关联")
    public R<Page<RegionDistributorVo>> pageList(@RequestBody RegionDistributorDto regionDistributorDto) {
        Page<RegionDistributorVo> page = regionDistributorService.pageList(regionDistributorDto);
        return R.ok(page);
    }

    @PostMapping("/save")
    @ApiOperation("保存")
    public R<RegionDistributorPo> saveInfo(@RequestBody RegionDistributorDto regionDistributorDto) {
        RegionDistributorPo regionDistributorPo = BeanUtil.toBean(regionDistributorDto, RegionDistributorPo.class);
        // 要判断关联的用户的Id是否已经关联了
        RegionDistributorPo userInfo = regionDistributorService.getByUserId(regionDistributorPo.getUserId());
        if (userInfo != null) {
            throw new WashException("已经关联了该用户");
        }
        regionDistributorService.save(regionDistributorPo);
        return R.ok(regionDistributorPo);
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public R<RegionDistributorPo> update(@RequestBody RegionDistributorDto regionDistributorDto) {
        RegionDistributorPo regionDistributorPo = BeanUtil.toBean(regionDistributorDto, RegionDistributorPo.class);
        regionDistributorService.updateById(regionDistributorPo);
        return R.ok(regionDistributorPo);
    }


    @GetMapping("/updateStatus")
    @ApiOperation("修改状态")
    public R<RegionDistributorPo> updateStatus(String id, Integer status) {
        RegionDistributorPo regionDistributorPo = regionDistributorService.getById(id);
        regionDistributorPo.setStatus(status);
        return R.ok(regionDistributorPo);
    }

}

